﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using BoolanSlide.RedoUndo;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace BoolanElement
{
    
    public class BooEllipseDialogElement:BooTextShapesBaseElement
    {

        Size arcsize;
        public Size ArcSize
        {
            get { return arcsize; }
            set { arcsize = value; }
        }

        Point arcStartPoint;
        public Point ArcStartPoint
        {
            get { return arcStartPoint; }
            set { arcStartPoint = value; }
        }

        Point arcEndPoint;
        public Point ArcEndPoint
        {
            get { return arcEndPoint; }
            set { arcEndPoint = value; }
        }

        Point line1Point;
        public Point Line1Point
        {
            get { return line1Point; }
            set { line1Point = value; }
        }

        Point line2Point;
        public Point Line2Point
        {
            get { return line2Point; }
            set { line2Point = value; }
        }

        public override BooElement Clone()
        {
            BooEllipseDialogElement element = new BooEllipseDialogElement();
            element.ElementTransform = this.ElementTransform.Clone();

            element.TextMarginLeft = this.TextMarginLeft;
            element.TextMarginTop = this.TextMarginTop;
            element.TextMaxHeight = this.TextMaxHeight;
            element.TextWidth = this.TextWidth;

            element.TextAlignment = this.TextAlignment;

            element.ShapeStyle = this.ShapeStyle;
            element.BorderColor = Color.FromArgb(this.BorderColor.A, this.BorderColor.R, this.BorderColor.G, this.BorderColor.B);
            element.BorderWidth = this.BorderWidth;

            element.ShapeFillColor = Color.FromArgb(this.ShapeFillColor.A, this.ShapeFillColor.R, this.ShapeFillColor.G, this.ShapeFillColor.B);
            element.ShapeWidth = this.ShapeWidth;
            element.ShapeHeight = this.ShapeHeight;

            element.ControlPanelTop = this.ControlPanelTop;
            element.ControlPanelLeft = this.ControlPanelLeft;
            element.Text = this.Text;

            element.arcStartPoint = new Point(this.ArcStartPoint.X, this.ArcStartPoint.Y);
            element.arcEndPoint = new Point(this.ArcEndPoint.X, this.ArcEndPoint.Y);
            element.arcsize = new Size(this.ArcSize.Width, this.ArcSize.Height);
            element.line1Point = new Point(this.Line1Point.X, this.Line1Point.Y);
            element.line2Point = new Point(this.Line2Point.X, this.Line2Point.Y);

            return element;
        }

        public override XElement SaveToXElement()
        {
            XElement element = new XElement(typeof(BooEllipseDialogElement).Name);
            element.SetAttributeValue("UniqueID", UniqueID.ToString());
            element.Add(ElementTransform.Save());
            XElement styleElement = SaveShapeStyle();

            XElement xArcSize = new XElement("ArcSize");
            xArcSize.SetAttributeValue("Width", this.arcsize.Width);
            xArcSize.SetAttributeValue("Height", this.arcsize.Height);
            XElement xArcStartPoint = new XElement("ArcStartPoint");
            xArcStartPoint.SetAttributeValue("X", this.arcStartPoint.X);
            xArcStartPoint.SetAttributeValue("Y", this.arcStartPoint.Y);
            XElement xArcEndPoint = new XElement("ArcEndPoint");
            xArcEndPoint.SetAttributeValue("X", this.arcEndPoint.X);
            xArcEndPoint.SetAttributeValue("Y", this.arcEndPoint.Y);
            XElement xLine1Point = new XElement("Line1Point");
            xLine1Point.SetAttributeValue("X", this.line1Point.X);
            xLine1Point.SetAttributeValue("Y", this.line1Point.Y);
            XElement xLine2Point = new XElement("Line2Point");
            xLine2Point.SetAttributeValue("X", this.line2Point.X);
            xLine2Point.SetAttributeValue("Y", this.line2Point.Y);
            
            element.Add(styleElement);
            element.Add(xArcSize);
            element.Add(xArcStartPoint);
            element.Add(xArcEndPoint);
            element.Add(xLine1Point);
            element.Add(xLine2Point);
           // element.Add
            element.Add(SaveData());

            return element;
        }

        public override void LoadFromXElement(XElement element)
        {
            XElement tranformElement=element.Element("Transform");
            XElement dataElement = element.Element("Data");
            XElement styleElement = element.Element("Style");

            ElementTransform.Load(tranformElement);
            LoadShapeStyle(styleElement);
            LoadData(dataElement);

            XElement xStartPoint = element.Element("ArcStartPoint");
            double xStartPointx = Double.Parse(xStartPoint.Attribute("X").Value);
            double xStartPointy = Double.Parse(xStartPoint.Attribute("Y").Value);
            this.arcStartPoint = new Point(xStartPointx, xStartPointy);

            XElement xEndPoint = element.Element("ArcEndPoint");
            double xEndPointx = Double.Parse(xEndPoint.Attribute("X").Value);
            double xEndPointy = Double.Parse(xEndPoint.Attribute("Y").Value);
            this.arcEndPoint = new Point(xEndPointx, xEndPointy);

            XElement xLine1Point = element.Element("Line1Point");
            double xLine1Pointx = Double.Parse(xLine1Point.Attribute("X").Value);
            double xLine1Pointy = Double.Parse(xLine1Point.Attribute("Y").Value);
            this.line1Point = new Point(xLine1Pointx, xLine1Pointy);

            XElement xLine2Point = element.Element("Line2Point");
            double xLine2Pointx = Double.Parse(xLine2Point.Attribute("X").Value);
            double xLine2Pointy = Double.Parse(xLine2Point.Attribute("Y").Value);
            this.line2Point = new Point(xLine2Pointx, xLine2Pointy);

            XElement xArcSize = element.Element("ArcSize");
            double xSizex = Double.Parse(xArcSize.Attribute("Width").Value);
            double xSizey = Double.Parse(xArcSize.Attribute("Height").Value);
            this.arcsize = new Size(xSizex, xSizey);
           
        }


        public static BooEllipseDialogElement InitElementStyle()
        {
            BooEllipseDialogElement ellipseDialogElement = new BooEllipseDialogElement();
            ellipseDialogElement.ElementTransform.Rotate.Angle = 0;
            ellipseDialogElement.ElementTransform.Rotate.CenterX = 0;
            ellipseDialogElement.ElementTransform.Rotate.CenterY = 0;

            ellipseDialogElement.ElementTransform.Scale.ScaleX = 1;
            ellipseDialogElement.ElementTransform.Scale.ScaleY = 1;
            ellipseDialogElement.ElementTransform.Scale.CenterX = 0;
            ellipseDialogElement.ElementTransform.Scale.CenterY = 0;

            ellipseDialogElement.ElementTransform.Translate.X = 0;
            ellipseDialogElement.ElementTransform.Translate.Y = 0;

            ellipseDialogElement.ElementTransform.Margin = new Windows.UI.Xaml.Thickness(0, 0, 0, 0);

            ellipseDialogElement.ElementTransform.Width = 200;
            ellipseDialogElement.ElementTransform.Height = 200;

            ellipseDialogElement.TextMarginLeft = 0;
            ellipseDialogElement.TextMarginTop = 0;
            ellipseDialogElement.TextMaxHeight = 200;
            ellipseDialogElement.TextWidth = 200;

            ellipseDialogElement.TextAlignment = SerializeCast.StringToTextAlignment("CenterCenter");

            ellipseDialogElement.ShapeStyle = SerializeCast.StringToTextBoxStyle("NoBorderBKG");
            ellipseDialogElement.BorderColor = Color.FromArgb(255, 0, 0, 0);
            ellipseDialogElement.BorderWidth = 0;

            ellipseDialogElement.ShapeFillColor = Color.FromArgb(255, 24, 122, 169);
            ellipseDialogElement.ShapeWidth = 200;
            ellipseDialogElement.ShapeHeight = 200;

            ellipseDialogElement.ControlPanelTop = 0;
            ellipseDialogElement.ControlPanelLeft = 0;

            ellipseDialogElement.arcStartPoint = new Point(150, 180);
            ellipseDialogElement.arcEndPoint = new Point(180, 165);
            ellipseDialogElement.arcsize = new Size(80, 50);
            ellipseDialogElement.line1Point = new Point(230, 200);
            ellipseDialogElement.line2Point = new Point(150, 180);

            ellipseDialogElement.Text = "";
            return ellipseDialogElement;
        }

        public override async Task<BooUIBase> Render(SaveOriginalElement saveOriginal, SaveForRedoUndo saveForRedoUndo)
        {
            BooEllipseDialog dialog = new BooEllipseDialog(this,saveForRedoUndo);
            RedoUndoHelper.SaveOriginalStatus(dialog);
            return dialog;
        }

        public override async Task<BooUIBase> RenderForThumbNails()
        {
            BooEllipseDialog dialog = new BooEllipseDialog(this);
            return dialog;
        }


        public override void Load(BooTextShapesBaseElement shapesElement)
        {
            BooEllipseDialogElement ele=shapesElement as BooEllipseDialogElement;
            this.Text = shapesElement.Text;
            this.TextAlignment = shapesElement.TextAlignment;
            this.TextMarginLeft = shapesElement.TextMarginLeft;
            this.TextMarginTop = shapesElement.TextMarginTop;
            this.TextMaxHeight = shapesElement.TextMaxHeight;
            this.TextWidth = shapesElement.TextWidth;
            this.UniqueID = shapesElement.UniqueID;
            this.ShapeStyle = shapesElement.ShapeStyle;
            this.ShapeHeight = shapesElement.ShapeHeight;
            this.ShapeFillColor = shapesElement.ShapeFillColor;
            this.ControlPanelTop = shapesElement.ControlPanelTop;
            this.ControlPanelLeft = shapesElement.ControlPanelLeft;
            this.BorderWidth = shapesElement.BorderWidth;
            this.BorderColor = shapesElement.BorderColor;
            this.ElementTransform = shapesElement.ElementTransform;
            this.Line1Point = ele.line1Point;
        }

    }
    
}
